import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from tkinter import messagebox


def goto_email(file_path, title):
    smtp_server = "smtp.qq.com"
    smtp_port = 465
    sender_email = "929463988@qq.com"  # 替换为你的QQ邮箱
    password = r"ulbibkjhjkakbfgb"  # 替换为QQ邮箱的授权码（不是密码！）
    with open(file_path, 'r', encoding='utf-8') as file:
        file_content = file.read()
    message = MIMEMultipart()
    message['From'] = sender_email
    message['To'] = "929463988@qq.com"
    message['Subject'] = title
    message.attach(MIMEText(file_content, 'plain'))
    attachment = MIMEText(file_content)
    attachment.add_header('Content-Disposition', 'attachment', filename="file.txt")
    message.attach(attachment)
    try:
        with smtplib.SMTP_SSL(smtp_server, smtp_port) as server:
            server.login(sender_email, password)
            server.send_message(message)
    except smtplib.SMTPResponseException as e:
        if e.smtp_code == -1:  # 如果是 QQ 邮箱的奇怪响应
            pass
        else:
            messagebox.showinfo('提醒', f"SMTP 错误: {e}")
    except Exception as e:
        messagebox.showinfo('提醒', f"邮件发送失败: {e}")